//
//  MainMenu.m
//  TestMenuSystem
//
//  Created by Ronald Ram on 12/21/12.
//  Copyright 2012 __MyCompanyName__. All rights reserved.
//

#import "MainMenu.h"
#import "NewGame.h"
#import "Options.h"
#import "Credits.h"

#import "SceneMgr.h"


#pragma mark - Initialization

@implementation MainMenu

- (id)init
{
    if (self = [super init] )
    {
        CGSize size = [CCDirector sharedDirector].winSize;
        
        //add BG to menu
        //CCLayerColor *bg = [CCLayerColor layerWithColor:ccc4(0, 128, 255, 255)];
        CCSprite *bg = [CCSprite spriteWithFile: @"Default.png"];
        bg.position = ccp(size.width * 0.5, size.height * 0.5);
        [self addChild: bg];
        
        //add menu title
        CCLabelTTF *label = [CCLabelTTF labelWithString: @"Main Menu" fontName: @"Noteworthy" fontSize: 56];
        //CGSize size = [CCDirector sharedDirector].winSize;
        label.position = ccp(size.width / 2, size.height - (size.height * 0.15) );
        [self addChild: label];
        
        //add MenuItem btns
        CCMenuItemFont *item1 = [CCMenuItemFont itemWithString:@"New Game" target:self selector:@selector(onClick:)];
        item1.tag = NEW_GAME;
        
        CCMenuItemFont *item2 = [CCMenuItemFont itemWithString:@"Options" target:self selector:@selector(onClick:)];
        item2.tag = OPTIONS;
        
        CCMenuItemFont *item3 = [CCMenuItemFont itemWithString:@"Credits" target:self selector:@selector(onClick:)];
        item3.tag = CREDITS;
        
        //add menuItem(s) to menu
        CCMenu *menu = [CCMenu menuWithItems:item1, item2, item3, nil];
        [menu alignItemsVerticallyWithPadding: 30];
        [menu setColor: ccORANGE];
        
        //add menu to layer
        [self addChild: menu];
        
        id action = [CCFadeIn actionWithDuration: 0.5];
        for ( id child in self.children)
        {
            [child runAction: action];
        }
    }
    return self;
}


#pragma mark - BBSelectionDelegate methods

+ (CCScene *) scene
{
    CCScene *scene = [CCScene node];
    CCLayer *menu = [MainMenu node];
    
    [scene addChild: menu];
    return scene;
}

- (void) onClick: (CCMenuItemFont *) sender
{
    id action = [CCFadeIn actionWithDuration: 0.0];
    
    switch (sender.tag)
    {
        case NEW_GAME:
//            [[CCDirector sharedDirector] replaceScene: [NewGame scene]];
//            [ [CCDirector sharedDirector] replaceScene: [CCTransitionMoveInR transitionWithDuration: 0.3 scene: [NewGame scene]] ];
            [[SceneMgr scene] switchLayer: [NewGame node] withAction: action];
            break;
        case OPTIONS:
//            [[CCDirector sharedDirector] replaceScene: [Options scene]];
//            [ [CCDirector sharedDirector] replaceScene: [CCTransitionMoveInR transitionWithDuration: 0.3 scene: [Options scene]] ];
            [[SceneMgr scene] switchLayer: [Options node] withAction: action];
            break;
        case CREDITS:
//            [[CCDirector sharedDirector] replaceScene: [Credits scene]];
//            [ [CCDirector sharedDirector] replaceScene: [CCTransitionMoveInR transitionWithDuration: 0.3 scene: [Credits scene]] ];
            [[SceneMgr scene] switchLayer: [Credits node] withAction: action];
            break;
        default:
//            [[CCDirector sharedDirector] replaceScene: [MainMenu scene]];
//            [ [CCDirector sharedDirector] replaceScene: [CCTransitionMoveInR transitionWithDuration: 0.3 scene: [MainMenu scene]] ];
            [[SceneMgr scene] switchLayer: [MainMenu node] withAction: action];
            break;
    }
}

@end
